<?php

if (! function_exists('admin_url')) {
    /**
     * Get admin url.
     *
     * @param  string $uri
     * @return string
     */
    function admin_url($uri)
    {
        return url('/dashboard/' . $uri);
    }
}

if(!function_exists('human_filesize')) {
    /**
     * Get a readable file size.
     *
     * @param $bytes
     * @param int $decimals
     * @return string
     */
    function human_filesize($bytes, $decimals = 2) {
        $size = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];

        $floor = floor((strlen($bytes)-1)/3);

        return sprintf("%.{$decimals}f", $bytes/pow(1024, $floor)).@$size[$floor];
    }
}

if(!function_exists('isActive')) {
    /**
     * Determine the nav if it is the current route.
     *
     * @param string $nav
     * @return boolean
     */
    function isActive($nav) {
        return Route::currentRouteName() == $nav ? 'active' : '';
    }
}

if(!function_exists('translug')) {
    /**
     * Translate the slug to english.
     *
     * @param string $slug
     * @return string
     */
    function translug($slug) {
        return app('Translug')->translate($slug);
    }
}

if(!function_exists('lang')) {
    /**
     * Trans for getting the language.
     *
     * @param string $text
     * @param  array $parameters
     * @return string
     */
    function lang($text, $parameters = [])
    {
        return trans('blog.'.$text, $parameters);
    }
}



if ( !function_exists('upload') ) {
    /**
     * 上传文件函数
     *
     * @param $file             表单的name名
     * @param string $path 上传的路径
     * @param bool $childPath 是否根据日期生成子目录
     * @return array            上传的状态
     */
    function upload($file, $path = 'upload', $childPath = true)
    {
        //判断请求中是否包含name=file的上传文件
        if (!request()->hasFile($file)) {
            $data = ['status_code' => 500, 'message' => '上传文件为空'];
            return $data;
        }
        $file = request()->file($file);
        //判断文件上传过程中是否出错
        if (!$file->isValid()) {
            $data = ['status_code' => 500, 'message' => '文件上传出错'];
            return $data;
        }

        // 存储图片
        if ($childPath == true) {
            $path = '/' . trim($path, './') . '/' . date('Ymd') . '/';
        } else {
            $path = '/' . trim($path, './') . '/';
        }
        $file_name = $path . md5(time() . uniqid()) . '.' . $file->extension();
        // 存储到七牛云
        $response_upload = \Storage::disk('qiniu')->put($file_name, fopen($file, 'r'));
        if ($response_upload === false) {
            return ['status_code' => 500, 'message' => '上传到七牛云失败'];
        }
//        $path = 'http://' . env('QINIU_DOMAIN');
        $path = 'http://img.miniooc.cn';

//		//获取上传的文件名
        $oldName = $file->getClientOriginalName();
//		//组合新的文件名
//		$newName = uniqid() . '.' . $file->getClientOriginalExtension();
//		//上传失败
//		if (!$file->move($path, $newName)) {
//			$data = ['status_code' => 500, 'message' => '保存文件失败'];
//			return $data;
//		}
        //上传成功
        $data = ['status_code' => 200, 'message' => '上传成功', 'data' => ['old_name' => $oldName, 'new_name' => $file_name, 'path' => $path]];
        return $data;
    }
}
